SUBROUTINE LAPS_CONFIG

!==============================================================================
!doc  THIS ROUTINE CONFIGURES LAPS IN ORDER TO INGEST DATA BY
!doc  LAPS LIBRARIES.
!doc
!doc  HISTORY:
!doc	CREATION:	YUANFU XIE	MAY 2007
!==============================================================================

  USE LAPS_PARAMS
  USE MEM_NAMELIST

  IMPLICIT NONE

  ! LOCAL VARIABLES:
  CHARACTER*4 :: VARNAM
  INTEGER :: STATUS

  ! VARIABLES FOR ACCESSING WIND PARAMETERS:
  CHARACTER*150 :: STATIC,FILENM
  INTEGER       :: LENGTH

  INTEGER :: NYEAR,NMONTH,NDAY,NHOUR,NMIN,NSEC

  FORMAT_REQUEST = 'BUFR'	! TESTING!!!

  ! SPATIAL DIM: GET_GRID_DIM_XY ALSO SETS LAPS_CONFIG:
  CALL GET_GRID_DIM_XY(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING XY DIMENSIONS', STATUS,NUMBER_GRIDPTS(1:2)
    STOP
  ENDIF
  CALL GET_LAPS_DIMENSIONS(NUMBER_GRIDPTS(3),STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING VERTICAL DIMENSION'
    STOP
  ENDIF

  ! SYSTEM TIME:
  CALL GET_SYSTIME(SYSTEM_IN4TIME,SYSTEM_ASCTIME,STATUS)
  !CALL GET_SYSTIME_ALL(SYSTEM_IN4TIME,SYSTEM_ASCTIME,ANALYS_HOURCHA, &
  !                     ANALYS_MINUTES,STRING_ASCTIME,YYYEAR_JULIANS, STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN GETTING SYSTEM TIME'
    STOP
  ENDIF
  ! CONVERT I4TIME TO YYYYMM_DDHHMIN:
  CALL CV_I4TIM_INT_LP(SYSTEM_IN4TIME,NYEAR,NMONTH,NDAY,NHOUR, &
                        NMIN,NSEC,STATUS)
  ! CALL READ_SYSTIM(STRING_ASCTIME,NUMBER_ASCTIME,YYYYMM_DDHHMIN,STATUS)
  YYYYMM_DDHHMIN(1) = 1900+NYEAR
  YYYYMM_DDHHMIN(2) = NMONTH
  YYYYMM_DDHHMIN(3) = NDAY
  YYYYMM_DDHHMIN(4) = NHOUR
  YYYYMM_DDHHMIN(5) = NMIN

  ! LAPS CYCLE TIME:
  CALL GET_LAPS_CYCLE_TIME(LENGTH_ANATIME,STATUS)
  LENGTH_ANATIME = 0.5*LENGTH_ANATIME
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING ANALYSIS TIME WINDOW'
    STOP
  ENDIF

  ! MISSING DATA:
  CALL GET_R_MISSING_DATA(RVALUE_MISSING,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING REAL MISSING VALUE'
    STOP
  ENDIF
  CALL GET_I2_MISSING_DATA(IVALUE_MISSING,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING INTEGER MISSING VALUE'
    STOP
  ENDIF
  CALL GET_SFC_BADFLAG(SFCOBS_INVALID,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING BAD VALUE FOR SURFACE'
    STOP
  ENDIF

  ! DOMAIN CONFIGURATION:
  CALL LAPS_MALLOC
  CALL READ_STATIC_GRID(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),'LAT',&
                        DOMAIN_LATITDE,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING LATITUDE'
    STOP
  ENDIF
  CALL READ_STATIC_GRID(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),'LON',&
                        DOMAIN_LONGITD,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING LONGITUDE'
    STOP
  ENDIF
  CALL READ_STATIC_GRID(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),'AVG',&
                        DOMAIN_TOPOGRP,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING LONGITUDE'
    STOP
  ENDIF
  ! NOTE: GRID SPACING CURRENTLY SHOWS THE SAME DISTANCE IN X AND Y.
  ! THUS GRID4D_SPACING'S FIRST ELEMENT USED IN GET_GRID_SPACING_ACTUAL:
  CALL GET_GRID_SPACING_ACTUAL( &
    DOMAIN_LATITDE((NUMBER_GRIDPTS(1)-1)/2+1, &
                   (NUMBER_GRIDPTS(2)-1)/2+1), &
    DOMAIN_LONGITD((NUMBER_GRIDPTS(1)-1)/2+1, &
                   (NUMBER_GRIDPTS(2)-1)/2+1), GRID4D_SPACING,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING GRID X-Y SPACING'
    STOP
  ENDIF
  GRID4D_SPACING(2) = GRID4D_SPACING(1)
  ! HEIGHT FIELD:
  VARNAM = 'HT'
  CALL GET_MODELFG_3D(SYSTEM_IN4TIME,VARNAM,NUMBER_GRIDPTS(1), &
                      NUMBER_GRIDPTS(2),NUMBER_GRIDPTS(3), &
                      HEIGHT_GRID3DM,STATUS)
  IF (STATUS .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING HEIGHT FIELD'
    STOP
  ENDIF
  ! PRESSURE LEVELS:
  CALL get_pres_1d(system_in4time,NUMBER_GRIDPTS(3),pressr_grid1dm,status)
  ! Temperature:
  varnam = 'T3'
  CALL get_modelfg_3d(system_in4time,varnam,NUMBER_GRIDPTS(1), &
                      NUMBER_GRIDPTS(2),NUMBER_GRIDPTS(3), &
                temptr_grid3dm,status)
  IF (status .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: error retrieving temperature'
    STOP
  ENDIF
  ! U wind:
  varnam = 'U3'
  CALL get_modelfg_3d(system_in4time,varnam,NUMBER_GRIDPTS(1), &
                      NUMBER_GRIDPTS(2),NUMBER_GRIDPTS(3), &
                uuwind_grid3dm,status)
  IF (status .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: error retrieving U wind'
    STOP
  ENDIF
  ! V wind:
  varnam = 'V3'
  CALL get_modelfg_3d(system_in4time,varnam,NUMBER_GRIDPTS(1), &
                      NUMBER_GRIDPTS(2),NUMBER_GRIDPTS(3), &
                vvwind_grid3dm,status)
  IF (status .NE. 1) THEN
    WRITE(6,*) 'LAPS_CONFIG: error retrieving V wind'
    STOP
  ENDIF
  ! write background temporarily:
  ! write(11,*) NUMBER_GRIDPTS(1:3)
  ! write(11,*) HEIGHT_GRID3DM,temptr_grid3dm,uuwind_grid3dm,vvwind_grid3dm
  ! write(11,*) DOMAIN_LATITDE,DOMAIN_LONGITD,DOMAIN_TOPOGRP,PRESSR_GRID1DM

  ! write(12,*) HEIGHT_GRID3DM,PRESSR_GRID1DM
  
  ! GET WIND PARAMETERS:
  ! CALL GET_WIND_PARMS(USEOBS_RAOBDAT,USEOBS_CLDRFWD,USEOBS_RADARWD, &
  !		      THRESH_RADAROB(1),THRESH_RADAROB(2),THRESH_RADAROB(3), &
  !		      WEIGHT_BKGWIND,WEIGHT_RADARWD,THRESH_RMSWIND, &
  !                    MAXNUM_PROFLRS,MAXLVL_PROFLRS,WEIGHT_OPTIONS,STATUS)
  ! IF (STATUS .NE. 1) THEN
  !   WRITE(6,*) 'LAPS_CONFIG: ERROR IN READING WIND PARAMETERS'
  ! ENDIF
 
  ! USE A NEW LAPS WIND PARAMETER SCHEME:
  CALL GET_DIRECTORY('static',STATIC,LENGTH)
  FILENM = STATIC(1:LENGTH)//'/wind.nl'
  CALL READ_NAMELIST_LAPS ('wind',FILENM)
  MAXNUM_PROFLRS = MAX_PR		! LAPS WIND PARAMETER MAX_PR;
  MAXLVL_PROFLRS = MAX_PR_LEVELS		! LAPS WIND PARAMETER MAX_PR_LVLS;

  MAXNUM_SONDES = MAX_SND_GRID
  MAXLVL_SONDES = MAX_SND_LEVELS

END SUBROUTINE LAPS_CONFIG


SUBROUTINE LAPS_MALLOC

!==============================================================================
!doc  THIS ROUTINE ALLOCATES MEMORY FOR LAPS VARIABLES.
!doc
!doc  HISTORY:
!doc	CREATION:	YUANFU XIE	MAY 2007
!==============================================================================

  USE LAPS_PARAMS

  IMPLICIT NONE

  ! LOCAL VARIABLE:
  INTEGER :: STATUS

  ! TWO DIMENSION ARRAYS:
  ALLOCATE(DOMAIN_LATITDE(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2)), &
           DOMAIN_LONGITD(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2)), &
	   DOMAIN_TOPOGRP(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2)), &
	   PRESSR_GRID1DM(NUMBER_GRIDPTS(3)), &
           HEIGHT_GRID3DM(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),  &
                          NUMBER_GRIDPTS(3)), &
           temptr_GRID3DM(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),  &
                          NUMBER_GRIDPTS(3)), &
           uuwind_GRID3DM(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),  &
                          NUMBER_GRIDPTS(3)), &
           vvwind_GRID3DM(NUMBER_GRIDPTS(1),NUMBER_GRIDPTS(2),  &
                          NUMBER_GRIDPTS(3)), STAT=STATUS)
  IF (STATUS .NE. 0) THEN
    WRITE(6,*) 'LAPS_MALLOC: ERROR IN ALLOCATING MEMORY FOR LAT/LON/TOPO'
    STOP
  ENDIF

END SUBROUTINE LAPS_MALLOC


SUBROUTINE LAPS_DALLOC

!==============================================================================
!doc  THIS ROUTINE ALLOCATES MEMORY FOR LAPS VARIABLES.
!doc
!doc  HISTORY:
!doc	CREATION:	YUANFU XIE	MAY 2007
!==============================================================================

  USE LAPS_PARAMS

  IMPLICIT NONE

  ! LOCAL VARIABLE:
  INTEGER :: STATUS

  ! TWO DIMENSION ARRAYS:
  DEALLOCATE(DOMAIN_LATITDE, DOMAIN_LONGITD, DOMAIN_TOPOGRP, PRESSR_GRID1DM, &
		HEIGHT_GRID3DM, temptr_grid3dm, uuwind_grid3dm, &
		vvwind_grid3dm,STAT=STATUS)
  IF (STATUS .NE. 0) THEN
    WRITE(6,*) 'LAPS_DALLOC: ERROR IN DEALLOCATING MEMORY FOR LAT/LON/TOPO'
    STOP
  ENDIF

END SUBROUTINE LAPS_DALLOC

